package com.magicsoft.share.ui.article;

import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.magicsoft.share.R;
import com.magicsoft.share.base.BaseActivity;
import com.magicsoft.share.constant.AppDavikActivityMgr;
import com.magicsoft.share.constant.SPKey;
import com.magicsoft.share.constant.Url;
import com.magicsoft.share.presenter.article.ArticlePreviewPresenter;
import com.magicsoft.share.ui.my.AllShareActivity;
import com.magicsoft.share.utils.SPUtils;
import com.magicsoft.share.utils.ToastUtils;
import com.magicsoft.share.view.article.IArticlePreviewView;
import com.makeramen.roundedimageview.RoundedImageView;

import butterknife.BindView;
import butterknife.OnClick;

public class ArticlePreviewActivity extends BaseActivity<IArticlePreviewView, ArticlePreviewPresenter> implements IArticlePreviewView {


    @BindView(R.id.view_top)
    View viewTop;
    @BindView(R.id.ibtn_left)
    ImageButton ibtnLeft;
    @BindView(R.id.iv_title_head)
    RoundedImageView ivTitleHead;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.layout_title)
    RelativeLayout layoutTitle;
    @BindView(R.id.view_title)
    View viewTitle;
    @BindView(R.id.btn_bottom)
    Button btnBottom;
    @BindView(R.id.view_bottom)
    View viewBottom;
    @BindView(R.id.webview)
    WebView webview;

    private String titleStr;
    private String contentStr;
    private String coverStr;
    private String coverRiginalStr;
    private Integer status=2;
    private Long articleId;

    @Override
    public ArticlePreviewPresenter initPresenter() {
        return new ArticlePreviewPresenter();
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_article_preview;
    }

    @Override
    public void initData() {

        titleStr = getIntent().getStringExtra("titleStr");
        contentStr = getIntent().getStringExtra("contentStr");
        coverStr = getIntent().getStringExtra("coverStr");
        coverRiginalStr = getIntent().getStringExtra("coverRiginalStr");

        long articleId = getIntent().getLongExtra("articleId", -1);
        if (articleId != -1) {
            this.articleId = articleId;
        } else {
            this.articleId = null;
        }
        Log.d("lyf", "initData: " + contentStr);
    }

    @Override
    public void initView() {

        tvName.setText(SPUtils.getInstance().getString(SPKey.NAME));
        Glide.with(this).load(SPUtils.getInstance().getString(SPKey.HEADIMG)).into(ivTitleHead);

        //需要访问的网址
        String url = Url.BASE_URL + "api/open/article/articlePreview";
       //post访问需要提交的参数
        String postDate = "title=" + titleStr + "&content=" + contentStr + "&nickname=" + SPUtils.getInstance().getString(SPKey.NAME);
       //由于webView.postUrl(url, postData)中 postData类型为byte[] ，
       //通过EncodingUtils.getBytes(data, charset)方法进行转换
        WebSettings webSettings = webview.getSettings();
        webSettings.setJavaScriptEnabled(true);
        // 设置允许JS弹窗
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
        webview.postUrl(url, postDate.getBytes());
        Log.e("TESTDATE",url+"  "+postDate);

    }

    @Override
    public void requestData() {

//          mPresenter.articlePreview(titleStr,contentStr);
    }

    @OnClick({R.id.ibtn_left, R.id.tv_right, R.id.btn_bottom})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ibtn_left:
                finish();
                break;
            case R.id.tv_right:
                mPresenter.articleEdit(articleId,titleStr, coverStr, coverRiginalStr, contentStr, null, null, null, null, status);

                break;
            case R.id.btn_bottom:
                Intent intent = new Intent(this, ArticleSettingActivity.class);
                intent.putExtra("titleStr", titleStr);
                intent.putExtra("contentStr", contentStr);
                intent.putExtra("coverStr", coverStr);
                intent.putExtra("coverRiginalStr", coverRiginalStr);
                intent.putExtra("articleId", articleId);

                startActivity(intent);
                break;
        }
    }

    @Override
    public void articlePreviewSuccess(String s) {

    }

    @Override
    public void articleEditSuccess() {
        ToastUtils.showToast(this, "文章存入草稿箱成功");
        AppDavikActivityMgr.getScreenManager().finishActivity(AddArticleActivity.class);
        Intent intent = new Intent(this, AllShareActivity.class);
        intent.putExtra("position", 1);
        startActivity(intent);
        finish();

    }


}
